package com.sc.nft.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.BoxItemDao;
import com.sc.nft.dao.BoxProductDao;
import com.sc.nft.dao.DigitalCollectionDao;
import com.sc.nft.dao.DigitalCollectionProductInfoDao;
import com.sc.nft.entity.BoxItem;
import com.sc.nft.entity.BoxProduct;
import com.sc.nft.entity.DigitalCollection;
import com.sc.nft.entity.DigitalCollectionProductInfo;
import com.sc.nft.entity.vo.BoxItemVO;
import com.sc.nft.entity.vo.CollectionSimpleVO;
import com.sc.nft.enums.ClassifyType;
import com.sc.nft.exception.GlobalRunTimeException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.BoxProductService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务接口实现
 *
 * @author Sun
 * @since 2022-05-30 17:30:36
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BoxProductServiceImpl implements BoxProductService {
    private final BoxProductDao boxProductDao;
    private final BoxItemDao boxItemDao;
    private final DigitalCollectionProductInfoDao digitalCollectionProductInfoDao;
    private final DigitalCollectionDao digitalCollectionDao;

    @Override
    public BoxProduct getByTitle(String title) {
        return boxProductDao.getByTitle(title);
    }

    @Override
    public BoxProduct getById(Long id) {
        return boxProductDao.getById(id);
    }

    @Override
    public Page<BoxProduct> page(String firstTitle, Boolean status, int pageNo, int pageSize) {
        return boxProductDao.getPage(firstTitle,status,pageNo,pageSize);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addItem(List<BoxItem> boxItems, Long boxId) {
        for (BoxItem boxItem : boxItems) {
            DigitalCollection digitalCollection = digitalCollectionDao.getById(boxItem.getCollectionId());
            if (digitalCollection.getUsableCount()<boxItem.getSumCount()){
                throw new GlobalRunTimeException("藏品库存不足");
            }
        }
        boxItemDao.clearItem(boxId);
        boxItemDao.saveBatch(boxItems);
    }

    @Override
    public List<BoxItemVO> getBoxItem(Long boxId) {
        return boxItemDao.getBoxItemVO(boxId);
    }

    @Override
    public List<BoxProduct> getCollectionSimpleVO(String firstTitle) {
        return boxProductDao.getCollectionSimpleVO(firstTitle);
    }

    @Override
    public void editBox(BoxProduct boxProduct1) {
        boxProduct1.updateById();
        List<DigitalCollectionProductInfo> byCollectionId = digitalCollectionProductInfoDao.selectByCollectionId(boxProduct1.getId(), ClassifyType.BLIND_BOX);
        for (DigitalCollectionProductInfo digitalCollectionProductInfo : byCollectionId) {
            digitalCollectionProductInfo.setCreationContent(boxProduct1.getCreationContent());
            digitalCollectionProductInfo.setProductDetailPicture(boxProduct1.getProductDetailPicture());
            digitalCollectionProductInfo.setProductDetailPictureType(boxProduct1.getProductDetailPictureType());
            digitalCollectionProductInfo.setProductPresentation(boxProduct1.getProductPresentation());
            digitalCollectionProductInfo.setThreeModelId(boxProduct1.getThreeModelId());
            digitalCollectionProductInfo.updateById();
        }
    }


}